package dao

import (
	"github.com/wonderivan/logger"
	"ruoyi-go/app/admin/entity"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/pkg/mysql"
	"ruoyi-go/utils"
	"ruoyi-go/utils/R"
	"strings"
	"time"
)

// 根据id获取model
func EdgeSensorLoadById(id int64) R.Result {

	if id == 0 {
		return R.ReturnFailMsg("参数获取失败")
	}
	var data entity.EdgeSensor
	err := mysql.MysqlDb().Where("id = ?", id).First(&data).Error
	if err != nil {
		return R.ReturnFailMsg("获取数据失败")
	} else {
		return R.ReturnSuccess(data)
	}
}

// 根据ID批量删除
func EdgeSensorDelete(ids string) R.Result {
	if len(ids) == 0 {
		return R.ReturnFailMsg("参数获取失败")
	}

	err := mysql.MysqlDb().Delete(&entity.EdgeSensor{}, "id in (?)", ids).Error
	if err != nil {
		return R.ReturnFailMsg("批量删除失败")
	}

	return R.ReturnSuccess(nil)
}

// 创建
func EdgeSensorSave(req entity.EdgeSensor) R.Result {

	req.CreateTime = utils.JsonTime{Time: time.Now()}
	req.UpdateTime = utils.JsonTime{Time: time.Now()}

	err := mysql.MysqlDb().Create(&req).Error
	if err != nil {
		logger.Error(err)
		return R.ReturnFailMsg("保存失败")
	}
	return R.ReturnSuccess(nil)
}

// 根据id 更新 ，排除零值
func EdgeSensorUpdate(req entity.EdgeSensor) R.Result {
	// 可以自定义判断
	req.UpdateTime = utils.JsonTime{Time: time.Now()}

	err := mysql.MysqlDb().Updates(&req).Error
	if err != nil {
		return R.ReturnFailMsg("更新失败")
	}
	return R.ReturnSuccess(nil)
}

// 不分页查询list
func GetListLimitEdgeSensorService(params tools.SearchTableDataParam) tools.TableDataInfo {

	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	var total int64
	db := mysql.MysqlDb().Table("edge_sensor")
	otherParams := params.Other.(entity.EdgeSensorPageReq)

	// 构建 WHERE 子句
	whereClauses := []string{}
	values := []interface{}{}
	if otherParams.SensorCode != "" {
		whereClauses = append(whereClauses, "sensor_code = ?")
		values = append(values, otherParams.SensorCode)
	}
	if otherParams.SensorName != "" {
		whereClauses = append(whereClauses, "sensor_name LIKE ?")
		values = append(values, "%"+otherParams.SensorName+"%")
	}
	if otherParams.SensorTypeId != "" {
		whereClauses = append(whereClauses, "sensor_type_id = ?")
		values = append(values, otherParams.SensorTypeId)
	}
	if otherParams.SensorType != "" {
		whereClauses = append(whereClauses, "sensor_type = ?")
		values = append(values, otherParams.SensorType)
	}
	if otherParams.SensorTypeName != "" {
		whereClauses = append(whereClauses, "sensor_type_name LIKE ?")
		values = append(values, "%"+otherParams.SensorTypeName+"%")
	}
	if otherParams.SensorModel != "" {
		whereClauses = append(whereClauses, "sensor_model = ?")
		values = append(values, otherParams.SensorModel)
	}
	if otherParams.SensorLocation != "" {
		whereClauses = append(whereClauses, "sensor_location LIKE ?")
		values = append(values, "%"+otherParams.SensorLocation+"%")
	}
	if otherParams.Status != "" {
		whereClauses = append(whereClauses, "status = ?")
		values = append(values, otherParams.Status)
	}
	if otherParams.Unit != "" {
		whereClauses = append(whereClauses, "unit = ?")
		values = append(values, otherParams.Unit)
	}
	if otherParams.Description != "" {
		whereClauses = append(whereClauses, "description LIKE ?")
		values = append(values, "%"+otherParams.Description+"%")
	}
	if otherParams.IsEnabled != 0 {
		whereClauses = append(whereClauses, "is_enabled = ?")
		values = append(values, otherParams.IsEnabled)
	}
	if otherParams.SensorField1 != "" {
		whereClauses = append(whereClauses, "sensor_field1 = ?")
		values = append(values, otherParams.SensorField1)
	}
	if otherParams.SensorField2 != "" {
		whereClauses = append(whereClauses, "sensor_field2 = ?")
		values = append(values, otherParams.SensorField2)
	}
	if otherParams.SensorField3 != "" {
		whereClauses = append(whereClauses, "sensor_field3 = ?")
		values = append(values, otherParams.SensorField3)
	}
	if otherParams.SensorField4 != "" {
		whereClauses = append(whereClauses, "sensor_field4 = ?")
		values = append(values, otherParams.SensorField4)
	}
	if otherParams.SensorField5 != "" {
		whereClauses = append(whereClauses, "sensor_field5 = ?")
		values = append(values, otherParams.SensorField5)
	}

	if beginTime != "" {
		whereClauses = append(whereClauses, "create_time >= ?")
		beginTimeStr := utils.GetBeginTime(beginTime)
		values = append(values, beginTimeStr)
	}

	if endTime != "" {
		whereClauses = append(whereClauses, "create_time <= ?")
		endTimeStr := utils.GetBeginTime(endTime)
		values = append(values, endTimeStr)
	}

	if len(whereClauses) > 0 {
		db = db.Where(strings.Join(whereClauses, " AND "), values...)
	}

	var rows []entity.EdgeSensor

	if err := db.Count(&total).Error; err != nil {
		return tools.Fail()
	}

	db.Order("id DESC").Find(&rows)
	if rows == nil {
		return tools.Fail()
	} else {
		return tools.Success(rows, total)
	}
}

// 按条件分页查询 EdgeSensorPage 分页查询EdgeSensor信息
func GetPageLimitEdgeSensorService(params tools.SearchTableDataParam) tools.TableDataInfo {
	var pageNum = params.PageNum
	var pageSize = params.PageSize
	var beginTime = params.Params.BeginTime
	var endTime = params.Params.EndTime

	var total int64
	db := mysql.MysqlDb().Table("edge_sensor")
	otherParams := params.Other.(entity.EdgeSensorPageReq)

	// 构建 WHERE 子句
	whereClauses := []string{}
	values := []interface{}{}
	if otherParams.SensorCode != "" {
		whereClauses = append(whereClauses, "sensor_code = ?")
		values = append(values, otherParams.SensorCode)
	}
	if otherParams.SensorName != "" {
		whereClauses = append(whereClauses, "sensor_name LIKE ?")
		values = append(values, "%"+otherParams.SensorName+"%")
	}
	if otherParams.SensorTypeId != "" {
		whereClauses = append(whereClauses, "sensor_type_id = ?")
		values = append(values, otherParams.SensorTypeId)
	}
	if otherParams.SensorType != "" {
		whereClauses = append(whereClauses, "sensor_type = ?")
		values = append(values, otherParams.SensorType)
	}
	if otherParams.SensorTypeName != "" {
		whereClauses = append(whereClauses, "sensor_type_name LIKE ?")
		values = append(values, "%"+otherParams.SensorTypeName+"%")
	}
	if otherParams.SensorModel != "" {
		whereClauses = append(whereClauses, "sensor_model = ?")
		values = append(values, otherParams.SensorModel)
	}
	if otherParams.SensorLocation != "" {
		whereClauses = append(whereClauses, "sensor_location LIKE ?")
		values = append(values, "%"+otherParams.SensorLocation+"%")
	}
	if otherParams.Status != "" {
		whereClauses = append(whereClauses, "status = ?")
		values = append(values, otherParams.Status)
	}
	if otherParams.Unit != "" {
		whereClauses = append(whereClauses, "unit = ?")
		values = append(values, otherParams.Unit)
	}
	if otherParams.Description != "" {
		whereClauses = append(whereClauses, "description LIKE ?")
		values = append(values, "%"+otherParams.Description+"%")
	}
	if otherParams.IsEnabled != 0 {
		whereClauses = append(whereClauses, "is_enabled = ?")
		values = append(values, otherParams.IsEnabled)
	}
	if otherParams.SensorField1 != "" {
		whereClauses = append(whereClauses, "sensor_field1 = ?")
		values = append(values, otherParams.SensorField1)
	}
	if otherParams.SensorField2 != "" {
		whereClauses = append(whereClauses, "sensor_field2 = ?")
		values = append(values, otherParams.SensorField2)
	}
	if otherParams.SensorField3 != "" {
		whereClauses = append(whereClauses, "sensor_field3 = ?")
		values = append(values, otherParams.SensorField3)
	}
	if otherParams.SensorField4 != "" {
		whereClauses = append(whereClauses, "sensor_field4 = ?")
		values = append(values, otherParams.SensorField4)
	}
	if otherParams.SensorField5 != "" {
		whereClauses = append(whereClauses, "sensor_field5 = ?")
		values = append(values, otherParams.SensorField5)
	}

	if beginTime != "" {
		whereClauses = append(whereClauses, "create_time >= ?")
		beginTimeStr := utils.GetBeginTime(beginTime)
		values = append(values, beginTimeStr)
	}

	if endTime != "" {
		whereClauses = append(whereClauses, "create_time <= ?")
		endTimeStr := utils.GetBeginTime(endTime)
		values = append(values, endTimeStr)
	}

	if len(whereClauses) > 0 {
		db = db.Where(strings.Join(whereClauses, " AND "), values...)
	}

	var rows []entity.EdgeSensor

	if err := db.Count(&total).Error; err != nil {
		return tools.Fail()
	}
	offset := (pageNum - 1) * pageSize
	db.Order("id DESC").Offset(offset).Limit(pageSize).Find(&rows)
	if rows == nil {
		return tools.Fail()
	} else {
		return tools.Success(rows, total)
	}
} 